package com.dawan.sbjy.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dawan.sbjy.Config.myExcep;
import com.dawan.sbjy.Rabbit.RabbitProducer;
import com.dawan.sbjy.dao.BaseDao;
import com.dawan.sbjy.dao.UserDao;
import com.dawan.sbjy.entity.User;
import com.dawan.sbjy.service.ifac.UserMapper;
import com.dawan.sbjy.util.WxdecryptDataUtil;
import org.bouncycastle.jce.provider.symmetric.ARC4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Service
public class UserService extends BaseDaoImpl<User> implements UserMapper {
    @Autowired
    UserDao userDao;
    @Autowired
    RabbitProducer rabbitProducer;
    @Value("${secret.salt}")
    private String salt;
    @Override
    public Map findUserById(String id) {
        return userDao.findUserById(id);
    }

    @Override
    public Map findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public String getPassword(String id) {
        return userDao.getPassword(id);
    }

    @Override
    public JSONObject setPhone(JSONObject param) {
        //获取用户的sessionkey,用来解密用户的手机号
        String userid=param.getJSONObject("storeuserinfo").getString("userid");
        //获取sessionley
        String sessionkey=userDao.getSessionkey(userid);
        //获取用户的手机信息
        JSONObject userphonedata= WxdecryptDataUtil.decryptData(sessionkey,param.getString("encryptedData"),param.getString("iv"));
        //绑定用户手机号
        userDao.updatePhone(userid,userphonedata.getString("purePhoneNumber"));
        return userphonedata;
    }

    @Override
    public Map findUserByPassword(String email, String password) {
        //根据邮箱去查找用户
        HashMap userinf= userDao.findUserByPassword(email);
        if(userinf!=null){
            String userpassword=(String) userinf.get("password");
            String md5password = DigestUtils.md5DigestAsHex((password+salt).getBytes());
            if(userpassword.equals(md5password)){
               return userinf;
            }else{
                throw new myExcep(500,"密码错误");
            }
        }else{
            throw new myExcep(500,"用户未注册");
        }
    }

    @Transactional
    @Override
    public int usergeiste(String code, String password, String email) {
        //判断邮箱和验证码的一致性
        int matchlength=  userDao.emailmatchcode(email,code);

        if(matchlength>0){
            //用户注册
            String md5Password = DigestUtils.md5DigestAsHex((password+salt).getBytes());
            userDao.registeuser(md5Password,email);
            //删除掉验证码
            userDao.deleteemailcode(email,code);
            return 1;
        }else{
            //验证码错误或已经超时
         throw new myExcep(500,"邮箱验证码错误或超时，请重新获取验证码！");
        }
    }

    public void userqueue(String email){
        rabbitProducer.send(email);
    }
}
